package com.tianqu.tms.www.settle.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferSettlementInfoDao;
import com.tianqu.tms.dto.TransferSettlementInfoDto;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferSettlementDate;
import com.tianqu.tms.entity.TransferSettlementInfo;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.www.settle.actionform.TransferSettleActionForm;
import com.tianqu.tms.www.settle.controller.TransferSettleSearchDisPatchController;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleSearchInputTopAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		TransferSettleActionForm inForm = (TransferSettleActionForm)form;
		TransferSettleViewHelper view = new TransferSettleViewHelper();
		if(uc.getAttribute(TmsConstants.VIEWHELP)!=null)
		{
			view = (TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		}
		if("2".equals(inForm.getSettleType()))
		{
			
			//向数据库中插数据 插完取
			TransferSettleSearchDisPatchController controller = new TransferSettleSearchDisPatchController();
			controller.execute(uc);
		}
		//查询当前用户
		TmsUserProfile profile = uc.getUserProfile();
		UserAuth auth = new UserAuth();
		boolean flag = auth.getAuthInfoByRole(profile.getUniqueId(), 470);
		String settleId =  view.getSettleId();
		if(flag)
		{
			//当前用户为收费专员 只显示自己
			if("3".equals(inForm.getSettleType()))
			{
				settleId =inForm.getSettlePeriodId(); 
				if(settleId.indexOf(")")>-1)
				{}else{
					settleId = "("+settleId+")";
				}
			}else{
				if(settleId.indexOf(")")>-1)
				{}else{
					settleId = "("+settleId+")";
				}
			}
			view.setRoleFlag("1");
		}else{
			//不是收费员的话直接获取组
			String settlePeriodId = inForm.getSettlePeriodId();
			if(inForm.getSettlePeriodId()==null || "".equals(inForm.getSettlePeriodId()))
			{
				settlePeriodId = view.getSettlePeriodId();
			}
			TransferSettlementDate dd = new TransferSettlementDate();
			String settleDateId = dd.getSettleDateIdBySettleDayId(settlePeriodId);
			settleId = "("+settleDateId+")";
			view.setRoleFlag("2");
			view.setSettlePeriodId(settlePeriodId);
		}
		//根据id到过户每日结算信息表中获取所有收费员信息
		TransferSettlementInfo info = new TransferSettlementInfo();
		List chargePerson = info.getChargePersonInfoBySettleId(settleId);
		view.setChargeList(chargePerson);
		//根据第一个收费员获取列表信息
		String strName = "";
		if(chargePerson!=null)
		{
			for(int j=0;j<chargePerson.size();j++)
			{
				Map map = (Map)chargePerson.get(j);
				strName+=map.get("realName")+" | ";
			}
			view.setStrName(strName);
			Map map = (Map)chargePerson.get(0);
			String str = (String)map.get("realName");
			String insertUserId = (String)map.get("insertUserId");
			if(!"0".equals(insertUserId))
			{
				//根据用户ID查询列表信息
				view.setChargeName(str);
				//查开始结束时间
				TransferSettlementDate settleDate =new TransferSettlementDate();
				settleDate = settleDate.getDateInfoBySettleId(settleId,Integer.parseInt(insertUserId));
				view.setStartDate(settleDate.getStartDate());
				view.setEndDate(settleDate.getEndDate());
			}
//			//流程表收费
//			TransferSettlementInfo getYPrice1 = new TransferSettlementInfo();
//			getYPrice1 = getYPrice1.getYPriceByUserId(insertUserId,1,settleId);
//			view.setYprice1(StringUtil.addDH(String.valueOf(getYPrice1.getYsPrice())));
//			view.setSprice1(StringUtil.addDH(String.valueOf(getYPrice1.getSsPrice())));
//			//交易票收费
//			TransferSettlementInfo getYPrice2 = new TransferSettlementInfo();
//			getYPrice2 = getYPrice2.getYPriceByUserId(insertUserId,3,settleId);
//			view.setYprice2(StringUtil.addDH(String.valueOf(getYPrice2.getYsPrice())));
//			view.setSprice2(StringUtil.addDH(String.valueOf(getYPrice2.getSsPrice())));
//			//待办收费
//			TransferSettlementInfo getYPrice3 = new TransferSettlementInfo();
//			getYPrice3 = getYPrice3.getYPriceByUserId(insertUserId,6,settleId);
//			view.setYprice3(StringUtil.addDH(String.valueOf(getYPrice3.getYsPrice())));
//			view.setSprice3(StringUtil.addDH(String.valueOf(getYPrice3.getSsPrice())));
//			//待办超期/过期收费
//			TransferSettlementInfo getYPrice4 = new TransferSettlementInfo();
//			getYPrice4 = getYPrice4.getYPriceByUserId(insertUserId,7,settleId);
//			view.setYprice4(StringUtil.addDH(String.valueOf(getYPrice4.getYsPrice())));
//			view.setSprice4(StringUtil.addDH(String.valueOf(getYPrice4.getSsPrice())));
//			//变更交易票收费
//			TransferSettlementInfo getYPrice5 = new TransferSettlementInfo();
//			getYPrice5 = getYPrice5.getYPriceByUserId(insertUserId,5,settleId);
//			view.setYprice5(StringUtil.addDH(String.valueOf(getYPrice5.getYsPrice())));
//			view.setSprice5(StringUtil.addDH(String.valueOf(getYPrice5.getSsPrice())));
//			//流程表退费
//			TransferSettlementInfo getYPrice6 = new TransferSettlementInfo();
//			getYPrice6 = getYPrice6.getYPriceByUserId(insertUserId,2,settleId);
//			view.setYprice6(StringUtil.addDH(String.valueOf(getYPrice6.getYsPrice())));
//			view.setSprice6(StringUtil.addDH(String.valueOf(getYPrice6.getSsPrice())));
//			int ylctf = getYPrice6.getYsPrice();
//			int slctf = getYPrice6.getSsPrice();
//			//交易票退费
//			TransferSettlementInfo getYPrice7 = new TransferSettlementInfo();
//			getYPrice7 = getYPrice7.getYPriceByUserId(insertUserId,4,settleId);
//			view.setYprice7(StringUtil.addDH(String.valueOf(getYPrice7.getYsPrice())));
//			view.setSprice7(StringUtil.addDH(String.valueOf(getYPrice7.getSsPrice())));
//			ylctf = ylctf + getYPrice7.getYsPrice();
//			slctf = slctf + getYPrice7.getSsPrice();
//			//总计：
//			TransferSettlementInfo getYPrice8 = new TransferSettlementInfo();
//			getYPrice8 = getYPrice8.getTotlePriceByUserId(insertUserId,settleId);
//			view.setYprice8(StringUtil.addDH(String.valueOf(getYPrice8.getYsPrice()-ylctf*2)));
//			view.setSprice8(StringUtil.addDH(String.valueOf(getYPrice8.getSsPrice()-slctf*2)));
//			
			view.setUserId(insertUserId);

			Codes code = Codes.getInstance();
			TransferSettlementInfoDao uDao = (TransferSettlementInfoDao) TmsDao.getInstance(TmsDao.TMS_TRANSFER_SETTLEMENT_INFO);
			List<TransferSettlementInfoDto> list = uDao.getPriceByUserId(Integer.parseInt(insertUserId), settleId);
			int sumReceivable = 0;
			int sumPaid = 0;
			int sumReceivableBack = 0;
			int sumPaidBack = 0;
			for (int n = 0; n < list.size(); n++) {
				TransferSettlementInfoDto dto = list.get(n);
				dto.setChargeName(view.getChargeName());
				dto.setStartDate(view.getStartDate());
				dto.setEndDate(view.getEndDate());
				dto.setNum(n+1);
				dto.setSettleTypeName(code.getItemValueByItemIdAndItemCode(Codes.CHARGE_TYPE_CHARGE, dto.getSettletype()));
				if(dto.getNum()%2==0) dto.setCssFlag(1); else dto.setCssFlag(2);
				sumReceivable = sumReceivable + dto.getReceivable();
				sumPaid = sumPaid + dto.getPaid();
				sumReceivableBack = sumReceivableBack + dto.getReceivableBack();
				sumPaidBack = sumPaidBack + dto.getPaidBack();
				dto.setReceivableStr(StringUtil.addDH(dto.getReceivable()));
				dto.setReceivableBackStr(StringUtil.addDH((dto.getReceivableBack())));
				dto.setPaidStr(StringUtil.addDH((dto.getPaid())));
				dto.setPaidBackStr(StringUtil.addDH((dto.getPaidBack())));
			}
			List<Codes> types = code.getCodesListByItemId(Codes.CHARGE_TYPE_CHARGE);
			Collections.sort(types, new Comparator<Codes>() {  
	            @Override  
	            public int compare(Codes o1, Codes o2) {  
	                return o1.getItemCode()>o2.getItemCode() ? 1 : -1;  
	            }  
	        });  
			
			List<TransferSettlementInfoDto> viewList = new ArrayList<TransferSettlementInfoDto>();
			for (int n = 0; n < types.size(); n++) {
				Codes tmpCode = types.get(n);
				boolean f = true;
				for (TransferSettlementInfoDto dto : list) {
					if(dto.getSettletype() == tmpCode.getItemCode()) {
						dto.setNum(n+1);
						if(dto.getNum()%2==0) dto.setCssFlag(1); else dto.setCssFlag(2);
						viewList.add(dto);
						f = false;
						break;
					} 
				}
				if(f) {
					TransferSettlementInfoDto dto = new TransferSettlementInfoDto();
					dto.setNum(n+1);
					dto.setChargeName(view.getChargeName());
					dto.setStartDate(view.getStartDate());
					dto.setEndDate(view.getEndDate());
					dto.setSettleTypeName(tmpCode.getItemValue());
					if(dto.getNum()%2==0) dto.setCssFlag(1); else dto.setCssFlag(2);
					dto.setReceivable(0);
					dto.setReceivableBack(0);
					dto.setPaid(0);
					dto.setPaidBack(0);
					dto.setReceivableStr("0");
					dto.setReceivableBackStr("0");
					dto.setPaidStr("0");
					dto.setPaidBackStr("0");
					viewList.add(dto);
				}
			}
			
			view.setInfo(viewList);
			view.setSumReceivable(StringUtil.addDH((sumReceivable)));
			view.setSumReceivableBack(StringUtil.addDH((sumReceivableBack)));
			view.setSumPaid(StringUtil.addDH((sumPaid)));
			view.setSumPaidBack(StringUtil.addDH((sumPaidBack)));
		}
		view.setSettleId(settleId);
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		request.setAttribute(TmsConstants.VIEWHELP, view);
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
